package org.ko.core.web.auth;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.ko.core.domain.auth.Permission;
import org.ko.core.logic.auth.PermissionSvc;
import org.ko.core.web.BaseController;
import org.ko.core.web.Result;
import org.ko.core.websupport.log.Log;
import org.ko.core.websupport.log.impl.LogUitls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 
 * PermissionAct
 * @author zengxm 药膏
 * @date 2014-9-19
 * @version 1.0.0
 */
@Controller
@RequestMapping("/auth/permission/")
public class PermissionAct extends BaseController{
	
	@Autowired
	private PermissionSvc service;
	
	@Override
	protected String getTemplatePath() {
		return "core/auth/permission/";
	}
	
	@RequiresPermissions("permission:list")
	@RequestMapping("list")
	public String list(ModelMap model){
		model.addAttribute("permissionList", service.findAll());
		return view(LIST);
	}
	
	@RequestMapping(value="add", method = RequestMethod.GET)
	public String add(ModelMap model,Long pid){
		//如果是添加子菜单,则将父菜单信息查询出来
		if(null != pid){
			model.addAttribute("parent",service.get(pid));
		}
		
		return view("add");
	}
	
	@Log(message="新增权限",category=Log.PERMISSION)
	@RequiresPermissions("permission:add")
	@RequestMapping(value="add", method = RequestMethod.POST)
	public String addDo(HttpServletRequest request, HttpServletResponse response, Permission bean){
		try {
			service.addDo(bean);
	
			String parentName = bean.getParent()==null?"无":bean.getParent().getName();
			LogUitls.putLog("权限名="+bean.getName()+"，父权限="+parentName);
			return "redirect:list.do";
		} catch (Exception e) {
			e.printStackTrace();
			LogUitls.error(e);
			return redirect("add.do");
		}
	}
	
	@RequiresPermissions("permission:edit")
	@RequestMapping(value="edit",method = RequestMethod.GET)
	public String edit(
			HttpServletRequest request,
			HttpServletResponse response,
			ModelMap model,
			@RequestParam(value = "id", required = true)Long id){
		model.addAttribute("entity",service.get(id));
		return view("edit");
	}
	
	/**
	 * 提交修改内容,完成后重定向到列表
	 * @param request
	 * @param response
	 * @param model
	 * @param bean
	 * @return
	 */
	@RequiresPermissions("permission:edit")
	@RequestMapping(value="edit",method = RequestMethod.POST)
	@Log(message="编辑权限",category=Log.PERMISSION)
	public String editDo(HttpServletRequest request,HttpServletResponse response,ModelMap model,Permission bean){
		
		try {
			service.addDo(bean);
		} catch (Exception e) {
			LogUitls.error(e);
		}
		
		return "redirect:list.do";
	}
	
	/**
	 * 批量删除指定ID记录
	 * @param request
	 * @param response
	 * @param model
	 * @param ids
	 * @return
	 */
	@RequiresPermissions("permission:delete")
	@RequestMapping(value="delete",method = RequestMethod.POST)
	@Log(message="删除权限，id={0}",params={"id"},category=Log.PERMISSION)
	public @ResponseBody Result delete(
			HttpServletRequest request,
			HttpServletResponse response,
			ModelMap model,
			@RequestParam(value = "ids", required = true)Long ids[]){
		System.out.println("删除："+ids);
		Result result = new Result();
		try {
			service.delete(ids);
			result.setMessage("操作成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		
		return result;
	}
	
	/**
	 * 打开为角色分配权限的页面
	 * 
	 * @param req
	 * @param model
	 * @param roleId
	 * @return
	 */
	@RequiresPermissions("permission:assignToRole")
	@RequestMapping(value="assignToRole",method = RequestMethod.GET)
	public String assignToRole(HttpServletRequest req, ModelMap model,
			@RequestParam(value = "roleId", required = true) Long roleId) {
		service.assignToRole(model, roleId);
		return view("permission_role");
	}
	
	/**
	 * 保存角色分配的权限
	 * @param req
	 * @param model
	 * @param permission
	 * @return
	 */
	@RequiresPermissions("permission:assignToRole")
	@Log(message="对角色分配权限",category=Log.PERMISSION)
	@RequestMapping(value="assignToRole",method = RequestMethod.POST)
	public @ResponseBody Result assignToRole(HttpServletRequest req, ModelMap model,
			@RequestParam(value = "roleId", required = true) Long roleId,
			@RequestParam(value = "ids", required = true) Long ids[]) {
		
		Result re = new Result();
		try {
			service.assignToRoleDo(model, roleId, ids);
			LogUitls.putLog("分配成功，角色id="+roleId+"，权限为"+Arrays.toString(ids));
		} catch (Exception e) {
			re.setSuccess(false);
			
			LogUitls.error(e);
		}
		return re;
		//return redirect("/auth/role/list.do");
	}
}